package com.nowc.week.w106_c;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class c {

    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter out = new PrintWriter(System.out);

    public static void main(String[] args) throws IOException {
        int T = Integer.parseInt(br.readLine());
        while (T-- > 0) {
            solve();
        }
        out.flush();
        out.close();
        br.close();
    }

    static void solve() throws IOException {
        String[] s = br.readLine().split(" ");
        int n = Integer.parseInt(s[0]);
        long l1 = Long.parseLong(s[1]);
        long r1 = Long.parseLong(s[2]);
        long l2 = Long.parseLong(s[3]);
        long r2 = Long.parseLong(s[4]);

        r1 = Math.min(l1 + 10, r1);
        r2 = Math.min(l2 + 10, r2);

        int[] a = new int[n + 1];
        s = br.readLine().split(" ");
        for (int i = 3; i <= n; i++) {
            a[i] = Integer.parseInt(s[i - 3]);
        }

        if (n == 3) {
            for (long i = l1; i <= r1; i++) {
                long x = i % 10;
                for (long j = l2; j <= r2; j++) {
                    if (((j * x) % 10) == a[3]) {
                        out.println(i + " " + j);
                        return;
                    }
                }
            }
        }
        for (long i = l1; i <= r1; i++) {
            long x = i % 10;
            for (long j = l2; j <= r2; j++) {
                if (((x * j) % 10) == a[3] && ((j * a[3]) % 10) == a[4]) {
                    out.println(i + " " + j);
                    return;
                }
            }
        }
        out.println(-1 + " " + -1);
    }
}
